package com.wu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.entity.Product;
import com.wu.mapper.ProductCategoryMapper;
import com.wu.mapper.ProductMapper;
import com.wu.service.ProductService;
import com.wu.vo.DataVo;
import com.wu.vo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wustudy
 * @since 2020-12-29
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public DataVo<ProductVo>findData(Integer page,Integer limit) {
        //1.先把data的其他信息给填充
        DataVo<ProductVo> dataVo = new DataVo<>();
        dataVo.setCode(0);//////----------------------------------------坑，layui的数据格式，code必须为0，要不然就不对，好恶心，我在这错了好久
        dataVo.setMsg("wuStudy");
//        dataVo.setCount(productMapper.selectCount(null));
        //6.进行分页,写完这个还一定要嫁一个拦截器啊，也就是config文件夹的MyBatisPlusConfig文件---------------------------------------------------------------------------
        IPage<Product> productIPage = new Page<>(page,limit);
        IPage<Product> result = productMapper.selectPage(productIPage, null);//根据分页查所有数据
        //还有就是我么们的result中还包含了数据的总记录数，所以我们6.上的那条也可以改为下面的语句--------------------
        dataVo.setCount((int) result.getTotal());

        //2.开始对dataVo中的data信息进行封装---------------------------------------------------
//        List<Product> productList = productMapper.selectList(null);。这时候可以根据分页去替换这个
        List<Product> productList = result.getRecords();
        List<ProductVo> productVoList = new ArrayList<>();
        for (Product product : productList) {
            ProductVo productVo = new ProductVo();
            //3.先对其进行复制
            BeanUtils.copyProperties(product,productVo);
            //4.根据商品的分类id查到对应的分类名称
            if (productCategoryMapper.selectById(product.getCategoryleveloneId())!=null){
                productVo.setCategorylevelone(productCategoryMapper.selectById(product.getCategoryleveloneId()).getName());
            }
            if (productCategoryMapper.selectById(product.getCategoryleveltwoId())!=null){
                productVo.setCategoryleveltwo(productCategoryMapper.selectById(product.getCategoryleveltwoId()).getName());
            }
            if (productCategoryMapper.selectById(product.getCategorylevelthreeId())!=null){
                productVo.setCategorylevelthree(productCategoryMapper.selectById(product.getCategorylevelthreeId()).getName());
            }
            //5.封装
            productVoList.add(productVo);
        }
        dataVo.setData(productVoList);

        return dataVo;
    }
}
